package com.liuning.graduationdemo.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuning.graduationdemo.common.Constants;
import com.liuning.graduationdemo.common.Result;
import com.liuning.graduationdemo.config.AuthAccess;
import com.liuning.graduationdemo.entity.Files;
import com.liuning.graduationdemo.entity.Menu;
import com.liuning.graduationdemo.entity.User;
import com.liuning.graduationdemo.mapper.FilesMapper;
import com.liuning.graduationdemo.mapper.RoleMapper;
import com.liuning.graduationdemo.mapper.RoleMenuMapper;
import com.liuning.graduationdemo.mapper.UserMapper;
import com.liuning.graduationdemo.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private IMenuService menuService;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private FilesMapper filesMapper;

    // StringRedisTemplate 专门用来操作字符串
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @AuthAccess
    @GetMapping("/front/all")
//    @Cacheable(value = "files",key = "'frontName'")
    public Result frontAll() {
        //1 从缓存获取数据
        String jsonStr = stringRedisTemplate.opsForValue().get(Constants.FILES_KEY);
        List<Files> files;
        //2 根据key取缓存数据为空
        if (StrUtil.isBlank(jsonStr)) {
            //3 从数据库中查询数据
            files = filesMapper.selectList(null);
            //4 将数据存到redis中
            stringRedisTemplate.opsForValue().set(Constants.FILES_KEY, JSONUtil.toJsonStr(files));
        }else {
            //从redis缓存中获取数据
            files = JSONUtil.toBean(jsonStr, new TypeReference<List<Files>>() {
            }, true);
        }

        return Result.success(files);
    }

    @GetMapping("/menus/{roleId}")
    public Result getMenus(@PathVariable(name = "roleId") Integer roleId) {
        List<Integer> menuIds = roleMenuMapper.selectByRoleId(roleId);
        List<Menu> roleMenuList = new ArrayList<>();

        //全部的menus数据
        List<Menu> menus = menuService.listByIds(menuIds);
        //先遍历一级菜单
        for (Menu parent:menus) {
            if (parent.getPid() == null) {
                //一级菜单
                List<Menu> childrenMenu = new ArrayList<>();
                for (Menu children:menus) {
                    if (Objects.equals(children.getPid(), parent.getId())) {
                        //二级菜单
                        childrenMenu.add(children);
                    }
                }
                parent.setChildren(childrenMenu);
                //说明是一级菜单
                roleMenuList.add(parent);
            }
        }

        return Result.success(roleMenuList);
    }

    @GetMapping("/index")
    public String index() {
        return "ok";
    }



}
